package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemDescMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jt.mapper.ItemMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
	
	@Autowired
	private ItemMapper itemMapper;
	@Autowired
	private ItemDescMapper itemDescMapper;
	/**
	 * 分页的sql
	 * select * from tb_item limit 0,20 -->起始位置,条数 [0,20)  [0,19]
	 * select * from tb_item limit 20,20 -->第二页
	 * select * from tb_item limit 40,20 -->第三页
	 *
	 * select * from tb_item limit (page-1)*每页多少个,20 -->第三页
	 */
	@Override
	//@Transactional//开启事务控制
	public EasyUITable findItemByPage(int page, int rows) {
		//1.手写分页
		/*
		long total = itemMapper.selectCount(null);
		int startIndex = (page-1)*rows;
		List<Item> records =itemMapper.findItemByPage(startIndex,rows);
		return new EasyUITable(total,records);
		*/

		//2.使用MyBatisPlus,需要写一个配置类MybatisPlusConfig配置分页拦截器
		IPage mpPage = new Page(page,rows);//页数 行数
		QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("updated");
		mpPage=itemMapper.selectPage(mpPage,queryWrapper);
		long total = mpPage.getTotal(); //获取记录总数
		List<Item> records = mpPage.getRecords();//获取当前页
		return new EasyUITable(total,records);
	}

	/**
	 * 在运行时没有id值,再入库之后需要回显,回显时能获取到id
	 */
	@Override
	//@Transactional//开启事务控制
	public void saveItem(Item item, ItemDesc itemDesc) {
		//Date date = new Date();
		//item.setStatus(1).setCreated(date).setUpdated(date);
		//mp用法:如果完成了主键自增,则自动实现数据的回显 ->把库里所有属性复制到item对象上
		item.setStatus(1);
		itemMapper.insert(item);
		//获取主键信息
		itemDesc.setItemId(item.getId());
		itemDescMapper.insert(itemDesc);
	}

	@Override
	@Transactional//开启事务控制
	public void updateItem(Item item, ItemDesc itemDesc) {
		itemMapper.updateById(item);
		itemDescMapper.updateById(itemDesc.setItemId(item.getId()));
	}

	@Override
	@Transactional//开启事务控制
	public void deleteItem(Long[] ids) {
		//方式1:使用MyBatisPlus
		List<Long> longIds = Arrays.asList(ids);//数组转化为集合
		itemMapper.deleteBatchIds(longIds);
		//方式2:手写sql
		//itemMapper.deleteItemByIds(ids);

		itemDescMapper.deleteBatchIds(longIds);
	}

	@Override
	@Transactional
	public void updateStatus(Long[] ids,Integer status) {
		//1.以MyBatisPlus的方式进行操作数据库
		/*Item item = new Item();
		item.setStatus(status);
		QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
		queryWrapper.in("id", ids);
		itemMapper.update(item,queryWrapper);//只修改状态码和updated*/

		//2.手写sql方式
		itemMapper.updateStatus(ids,status);
	}

	@Override
	public ItemDesc findItemDescById(Integer itemId) {
		return itemDescMapper.selectById(itemId);
	}


}
